using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;

namespace Wantn.Share
{
	public sealed class ImgOpt
	{
		public static void ConfigGraphic(Graphics g)
		{
			g.SmoothingMode = SmoothingMode.HighQuality;
			g.CompositingQuality = CompositingQuality.HighQuality;
			g.TextRenderingHint = TextRenderingHint.AntiAlias;
			g.InterpolationMode = InterpolationMode.HighQualityBicubic;
		}

		private static string smethod_0(ImageEncodeType imageEncodeType_0)
		{
			if (imageEncodeType_0 == ImageEncodeType.Bmp)
			{
				return "image/bmp";
			}
			return "image/jpeg";
		}

		public static void SaveImage(Bitmap bmp, out MemoryStream ms, ImageEncodeType imgEncode, long quality)
		{
			ms = null;
			try
			{
				string b = smethod_0(imgEncode);
				ImageCodecInfo imageCodecInfo = null;
				ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
				for (int i = 0; i < imageEncoders.Length; i++)
				{
					if (imageEncoders[i].MimeType == b)
					{
						imageCodecInfo = imageEncoders[i];
						break;
					}
				}
				if (imageCodecInfo == null)
				{
					throw new Exception("找不到匹配的图像编码器");
				}
				EncoderParameters encoderParameters = new EncoderParameters(1);
				encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
				ms = new MemoryStream();
				bmp.Save(ms, imageCodecInfo, encoderParameters);
			}
			catch (Exception ex)
			{
				if (ms != null)
				{
					ms.Close();
					ms.Dispose();
					ms = null;
				}
				throw ex;
			}
		}

		public static void SaveImage(Bitmap bmp, string dstFile, ImageEncodeType imgEncode, long quality)
		{
			MemoryStream ms = null;
			FileStream fileStream = null;
			try
			{
				SaveImage(bmp, out ms, imgEncode, quality);
				fileStream = new FileStream(dstFile, FileMode.OpenOrCreate);
				fileStream.Write(ms.ToArray(), 0, (int)ms.Length);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (ms != null)
				{
					ms.Close();
					ms.Dispose();
				}
				if (fileStream != null)
				{
					fileStream.Close();
					fileStream.Dispose();
				}
			}
		}

		public static void SaveImage(string srcFile, string dstFile, ImageEncodeType imgEncode, long quality)
		{
			Image image = null;
			Bitmap bitmap = null;
			Graphics graphics = null;
			try
			{
				image = Image.FromFile(srcFile);
				bitmap = new Bitmap(image);
				bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
				graphics = Graphics.FromImage(bitmap);
				ConfigGraphic(graphics);
				graphics.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
				SaveImage(bitmap, dstFile, imgEncode, quality);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				image?.Dispose();
				bitmap?.Dispose();
				graphics?.Dispose();
			}
		}

		public static void SaveImage(string srcFile, string dstFile, ImageEncodeType imgEncode, long quality, int dstWidth, int dstHeight)
		{
			Image image = null;
			Bitmap bitmap = null;
			Graphics graphics = null;
			try
			{
				image = Image.FromFile(srcFile);
				int width = image.Width;
				int height = image.Height;
				if (dstWidth <= 0)
				{
					dstWidth = width;
				}
				if (dstHeight <= 0)
				{
					dstHeight = height;
				}
				bitmap = new Bitmap(dstWidth, dstHeight);
				graphics = Graphics.FromImage(bitmap);
				ConfigGraphic(graphics);
				Rectangle destRect = new Rectangle(0, 0, dstWidth, dstHeight);
				graphics.DrawImage(image, destRect, 0, 0, width, height, GraphicsUnit.Pixel);
				SaveImage(bitmap, dstFile, imgEncode, quality);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				image?.Dispose();
				bitmap?.Dispose();
				graphics?.Dispose();
			}
		}

		public Image SaveImage(byte[] binaryImg)
		{
			Image image = null;
			MemoryStream memoryStream = null;
			try
			{
				memoryStream = new MemoryStream(binaryImg);
				memoryStream.Position = 0L;
				return Image.FromStream(memoryStream);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (memoryStream != null)
				{
					memoryStream.Close();
					memoryStream.Dispose();
				}
			}
		}

		public Image SaveImage(Image imgSrc, int width, int height)
		{
			try
			{
				return imgSrc.GetThumbnailImage(width, height, null, IntPtr.Zero);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

	}
}
